﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using SystemFrameWork.Enums;
using SystemFrameWork;
using Entity.Model;

namespace DutyWeb
{
    public class BasePage : System.Web.UI.Page
    {
       
        /// <summary>
        /// 
        /// </summary>
        protected DepartmentManagement departmentManagement = new DepartmentManagement();

        /// <summary>
        /// 
        /// </summary>
        protected Sys_UserManagement sys_UserManagement = new Sys_UserManagement();

        /// <summary>
        /// 
        /// </summary>
        protected Sys_RoleManagement sys_RoleManagement = new Sys_RoleManagement();

       
        public int PageSize
        {
            get
            {
                return ConfigurationHelper.PageSize;
            }
        }

        protected int CurrentTotalRecord
        {
            get
            {
                return (int)ViewState["TotalRecord"];
            }

            set
            {
                ViewState["TotalRecord"] = value;
            }
        }

        /// <summary>
        /// Get the Current user group
        /// </summary>
        /// Modified by Li Xing
        public List<Sys_Role> CurrentRole
        {
            get
            {
                return UserSession.CurrentSession.CurrentRole;
            }

            set
            {
                UserSession.CurrentSession.CurrentRole = value;
            }
        }

        /// <summary>
        /// current user id
        /// </summary>
        public Guid CurrentUserID
        {
            get
            {
                try
                {
                    return new Guid(System.Web.HttpContext.Current.User.Identity.Name);
                }
                catch
                {
                    return Guid.Empty;
                }
            }
        }

        /// <summary>
        /// the data permission flag of the Sys_Role
        /// </summary>
        /// add by LiXing,2012-02-19
        public bool EnableDataPermission
        {
            get
            {
                return UserSession.CurrentSession.EnableDataPermission;
            }

            set
            {
                UserSession.CurrentSession.EnableDataPermission = value;
            }
        }

        /// <summary>
        /// Show message to client
        /// This method is a part of framework, depending on the ExceptionLevel
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="level"></param>
        public virtual void ShowMessage(string msg, ExceptionLevel level)
        {
            string messageIcon = "MessageBoxIcon.Information";

            switch (level)
            {
                case ExceptionLevel.Error:
                case ExceptionLevel.Fatal:
                    messageIcon = "MessageBoxIcon.Error";
                    break;
                case ExceptionLevel.Info:
                    messageIcon = "MessageBoxIcon.Information";
                    break;
                default:
                    messageIcon = "MessageBoxIcon.Warning";
                    break;
            }

            if (ScriptManager.GetCurrent(this) != null)
            {
                string script = string.Format(
                        "MessageBox.RelativePath='../Include/_js/';MessageBox.Show('{0}','{1}',{2},{3},{4});",
                        PageHelper.EncodeScriptParameter(msg),
                        PageHelper.EncodeScriptParameter(ResourceManager.GetString(ResourceManager.Title_MessageTitle, null)),
                        "MessageBoxButtons.OK",
                        messageIcon,
                        "DefaultMessageBoxButton.Button1");

                ScriptManager.RegisterStartupScript(
                    this,
                    this.GetType(),
                    "Message",
                    script,
                    true);
            }
            else
            {
                string script = string.Format(
                        "MessageBox.RelativePath='../Include/_js/';MessageBox.Show('{0}','{1}',{2},{3},{4});",
                        PageHelper.EncodeScriptParameter(msg),
                        PageHelper.EncodeScriptParameter(ResourceManager.GetString(ResourceManager.Title_MessageTitle, null)),
                        "MessageBoxButtons.OK",
                        messageIcon,
                        "DefaultMessageBoxButton.Button1");

                ClientScript.RegisterStartupScript(
                    this.GetType(),
                    "Message",
                    script,
                    true);
            }
        }

        /// <summary>
        /// respond a client method to the web page 
        /// </summary>
        /// <param name="parameter"></param>
        public virtual void ClientMethod(string parameter)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Message", parameter, false);
        }

        /// <summary>
        /// Get the check code
        /// </summary>
        public string CheckCode
        {
            get
            {
                return UserSession.CurrentSession.VerificationCode;
            }
            set
            {
                UserSession.CurrentSession.VerificationCode = value;
            }
        }
    }
}
